#include<bits/stdc++.h>
using namespace std;

int T;  

bool cmp(const vector<int>& a, const vector<int>& b) {
    return a[1] < b[1]; // 正确：返回 bool 类型
}//定义排序函数

void solve() {
    int n;
    cin >> n;
    vector<vector<int>> task(n, vector<int>(3));
    
    for (int i = 0; i < n; ++i) {
        int t, d, p;  
        cin >> t >> d >> p;  
        task[i] = {t, d, p}; 
    }
    
    sort(task.begin(), task.end(),cmp);//按截止时间从小到大排序
    
    vector<int> dp(5001, 0);
    for (int i = 0; i < n; ++i) {
        int t = task[i][0], d = task[i][1], p = task[i][2]; 
        for (int j = d; j >= t; --j) {
            dp[j] = max(dp[j], dp[j - t] + p);  // 0-1背包逻辑
        }
        for (int j = 1; j <= 5000; ++j) {
            dp[j] = max(dp[j], dp[j - 1]);
        }
    }
    
    cout << dp[5000] << endl;
}

int main() {
    cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}